{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding=utf-8\n",
    "# url配置参数\n",
    "\n",
    "from tornado import web\n",
    "import tornado\n",
    "\n",
    "\n",
    "class MainHandler(web.RequestHandler):\n",
    "    async def get(self, *args, **kwargs):\n",
    "\n",
    "        self.redirect(self.reverse_url('people_id', '99'))\n",
    "\n",
    "\n",
    "class PeopleIdHandler(web.RequestHandler):\n",
    "    def initialize(self, name):\n",
    "        self.db_name = name\n",
    "\n",
    "    async def get(self, id, *args, **kwargs):\n",
    "        self.write('user id: {}'.format(id))\n",
    "        # self.redirect(self.reverse_url('people_name', 'bobby'))\n",
    "\n",
    "\n",
    "class PeopleNameHandler(web.RequestHandler):\n",
    "    async def get(self, name, *args, **kwargs):\n",
    "        self.write('user name: {}'.format(name))\n",
    "\n",
    "\n",
    "class PeopleInfoHandler(web.RequestHandler):\n",
    "    async def get(self, name, age, gender, *args, **kwargs):\n",
    "        self.write('user name: {},age: {},gender: {}'.format(name, age, gender))\n",
    "\n",
    "\n",
    "people_db = {\n",
    "    'name': 'people'\n",
    "}\n",
    "\n",
    "urls = [\n",
    "    web.URLSpec('/', MainHandler, name='index'),\n",
    "    ## /people/1\n",
    "    web.URLSpec('/people/(\\d+)/?', PeopleIdHandler, people_db, name='people_id'),\n",
    "    web.URLSpec('/people/(\\w+)/?', PeopleNameHandler, name='people_name'),\n",
    "    ## 添加说明 ?P<param_name> 必须和参数一样\n",
    "    web.URLSpec('/people/(?P<name>\\w+)/(?P<age>\\d+)/(?P<gender>\\w+)/?', PeopleInfoHandler, name='people_info'),\n",
    "]\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app = web.Application(urls, debug=True, autoreload=True)\n",
    "    app.listen(8888)\n",
    "    tornado.ioloop.IOLoop.current().start()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
